package com.example.administrator.szb.activity;

import android.os.Bundle;
import android.support.annotation.IdRes;
import android.support.v4.app.FragmentTransaction;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.androidkun.xtablayout.XTabLayout;
import com.example.administrator.szb.R;
import com.example.administrator.szb.activity.base.BasePresenter;
import com.example.administrator.szb.activity.base.MVPBaseFragmentActivity;
import com.example.administrator.szb.bean.SearchBean;
import com.example.administrator.szb.tinkerutil.SampleApplicationLike;
import com.example.administrator.szb.fragments.search.SearchANli;
import com.example.administrator.szb.fragments.search.SearchFaLv;
import com.example.administrator.szb.fragments.search.SearchJG;
import com.example.administrator.szb.fragments.search.SearchWD;
import com.example.administrator.szb.fragments.search.SearchZX;
import com.example.administrator.szb.http.HttpUtil;
import com.example.administrator.szb.http.HttpUtils;
import com.example.administrator.szb.http.OnResultListener;
import com.example.administrator.szb.http.URLAddress;
import com.example.administrator.szb.util.DialogUtil;

import java.util.HashMap;

public class Search2Activity extends MVPBaseFragmentActivity {
    SearchJG searchJG;
    SearchWD searchWD;
    SearchZX searchZX;
    SearchANli searchANli;
    SearchFaLv searchFaLv;
    RadioGroup search_radioGroup;
    RadioButton search_radiobutton_jrgw,search_radiobutton_hyyh,search_radiobutton_zx,search_radiobutton_anli,search_radiobutton_falv;
    EditText search_edit_srnr;
    SearchBean dataBean_search=null;
    int selectTab = 0;
    private XTabLayout tabLayout;
    private boolean canReq = true;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search2);
    }

    @Override
    protected BasePresenter createPresenter() {
        return null;
    }

    @Override
    protected void initView() {
        tabLayout = findViewById(R.id.tabLayout2);
        search_radiobutton_anli = findViewById(R.id.search_radiobutton_anli);
        search_radiobutton_falv = findViewById(R.id.search_radiobutton_falv);
        search_radioGroup  = findViewById(R.id.search_radioGroup);
        search_edit_srnr = findViewById(R.id.search_edit_srnr);
        search_radiobutton_jrgw = findViewById(R.id.search_radiobutton_jrgw);
        search_radiobutton_hyyh = findViewById(R.id.search_radiobutton_hyyh);
        search_radiobutton_zx = findViewById(R.id.search_radiobutton_zx);
        search_edit_srnr.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                canReq=true;
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }

    @Override
    protected void initEvent() {
        search_edit_srnr.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            /** * @param v * @param actionId 针对软键盘,若是实体键盘actionId=0 * @param event 针对实体键盘,若是软键盘event=null * @return 返回true表示自己处理Enter事件,当imeOptions="actionSearch"时返回false此方法会被调用两次 */
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_SEARCH || event.getKeyCode() == KeyEvent.ACTION_DOWN) {
                    String str = search_edit_srnr.getText().toString().trim();
                    if (!TextUtils.isEmpty(str)){
                        requestData(str);
                    }

                    return true;
                }
                return false;
            }
        });
        search_radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, @IdRes int i) {
                switch (i){
                    case R.id.search_radiobutton_jrgw:
                        selectTab = 0;
                        initFragment(selectTab);
                        break;
                    case R.id.search_radiobutton_hyyh:
                        selectTab = 1;
                        initFragment(selectTab);
                        break;
                    case R.id.search_radiobutton_zx:
                        selectTab = 2;
                        initFragment(selectTab);
                        break;
                    case R.id.search_radiobutton_anli:
                        selectTab = 3;
                        initFragment(selectTab);
                        break;
                    case R.id.search_radiobutton_falv:
                        selectTab = 4;
                        initFragment(selectTab);
                        break;
                }
            }
        });
        tabLayout.addTab(tabLayout.newTab().setText("机构"));
        tabLayout.addTab(tabLayout.newTab().setText("问答"));
        tabLayout.addTab(tabLayout.newTab().setText("投行资讯"));
        tabLayout.addTab(tabLayout.newTab().setText("案例学习"));
        tabLayout.addTab(tabLayout.newTab().setText("法律法规"));

        tabLayout.setOnTabSelectedListener(new XTabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(XTabLayout.Tab tab) {
                selectTab = tab.getPosition();
                initFragment(selectTab);
            }

            @Override
            public void onTabUnselected(XTabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(XTabLayout.Tab tab) {

            }
        });
    }

    @Override
    protected void initData() {

    }
    /**
     * 显示Fragment
     * @param i
     */
    private void initFragment(int i) {

        FragmentTransaction transaction = getFragmentTransaction();

        hideFragment(transaction);

        switch (i) {
            case 0:
                //首页
                if (searchJG!=null){
                    if (dataBean_search!=null){
                        searchJG.setDatas(dataBean_search.getData().getCounList());
                    }

                    transaction.show(searchJG);
                }else {
                    searchJG = new SearchJG();
                    if (dataBean_search!=null){
                        searchJG.setDatas(dataBean_search.getData().getCounList());
                    }
                    transaction.add(R.id.content,searchJG);
                }

                break;
            case 1:
                //问答
                if (searchWD!=null){
                    if (dataBean_search!=null){
                        searchWD.setDatas(dataBean_search.getData().getReferList());
                    }
                    transaction.show(searchWD);
                }else {
                    searchWD = new SearchWD();
                    if (dataBean_search!=null){
                        searchWD.setDatas(dataBean_search.getData().getReferList());
                    }
                    transaction.add(R.id.content,searchWD);

                }
                break;
            case 2:
                //资讯
                if (searchZX!=null){
                    if (dataBean_search!=null){
                        searchZX.setDatas(dataBean_search.getData().getArtiList());
                    }
                    transaction.show(searchZX);
                }else {
                    searchZX = new SearchZX();
                    if (dataBean_search!=null){
                        searchZX.setDatas(dataBean_search.getData().getArtiList());
                    }
                    transaction.add(R.id.content,searchZX);

                }
                break;
            case 3:
                //案例
                if (searchANli!=null){
                    if (dataBean_search!=null){
                        searchANli.setDatas(dataBean_search.getData().getAnliList());
                    }
                    transaction.show(searchANli);
                }else {
                    searchANli = new SearchANli();
                    if (dataBean_search!=null){
                        searchANli.setDatas(dataBean_search.getData().getAnliList());
                    }
                    transaction.add(R.id.content,searchANli);

                }
                break;
            case 4:
                //法律
                if (searchFaLv!=null){
                    if (dataBean_search!=null){
                        searchFaLv.setDatas(dataBean_search.getData().getLegalList());
                    }
                    transaction.show(searchFaLv);
                }else {
                    searchFaLv = new SearchFaLv();
                    if (dataBean_search!=null){
                        searchFaLv.setDatas(dataBean_search.getData().getLegalList());
                    }
                    transaction.add(R.id.content,searchFaLv);

                }
                break;

        }

        selectTab=i;
        transaction.commitAllowingStateLoss();
    }
    private void hideFragment(FragmentTransaction transaction){
        if (searchJG!=null){
            transaction.hide(searchJG);
        }
        if (searchWD!=null){
            transaction.hide(searchWD);
        }
        if (searchZX!=null){
            transaction.hide(searchZX);
        }
        if (searchANli!=null){
            transaction.hide(searchANli);
        }
        if (searchFaLv!=null){
            transaction.hide(searchFaLv);
        }
    }

    private void requestData(String param) {
        if (!canReq){
            return;
        }
        canReq = false;
        DialogUtil.showIsoProgressbar(context,"正在搜索");
        HashMap<String, Object> map = new HashMap<>();
        map.put("code", HttpUtil.getParam());
        map.put("param", param);
        //map.put("id", SampleApplicationLike.getUserloginInstance().getId() + "");
        map.put("users_id", SampleApplicationLike.getUserloginInstance().getId() + "");
        //map.put("type", type + "");


        Class clazz = SearchBean.class;

        String url =  URLAddress.URLBASE + URLAddress.URL_SEARCH;
        HttpUtils.post2(activity,url, map, 0, new OnResultListener() {
            @Override
            public void onSuccess(int requestCode, String str) {
                DialogUtil.dialog_ios.dismiss();
                canReq = true;
                dataBean_search = JSON.parseObject(str,new TypeReference<SearchBean>(){});
                //changActivity();
                search_radiobutton_jrgw.setText("机构\n(0)");
                search_radiobutton_hyyh.setText("问答\n(0)");
                search_radiobutton_zx.setText("资讯\n(0)");
                search_radiobutton_anli.setText("案例学习\n(0)");
                search_radiobutton_falv.setText("法律法规\n(0)");
                if (dataBean_search.getData().getCounList() != null && dataBean_search.getData().getCounList().size() >= 0) {
                    search_radiobutton_jrgw.setText("机构\n(" + dataBean_search.getData().getCounList().size() + ")");
                }
                if (dataBean_search.getData().getReferList() != null && dataBean_search.getData().getReferList().size() >= 0) {
                    search_radiobutton_hyyh.setText("问答\n(" + dataBean_search.getData().getReferList().size() + ")");
                }
                if (dataBean_search.getData().getArtiList() != null && dataBean_search.getData().getArtiList().size() >= 0) {
                    search_radiobutton_zx.setText("资讯\n(" + dataBean_search.getData().getArtiList().size() + ")");
                }
                if (dataBean_search.getData().getAnliList() != null && dataBean_search.getData().getAnliList().size() >= 0) {
                    search_radiobutton_anli.setText("案例学习\n(" + dataBean_search.getData().getAnliList().size() + ")");
                }
                if (dataBean_search.getData().getLegalList() != null && dataBean_search.getData().getLegalList().size() >= 0) {
                    search_radiobutton_falv.setText("法律法规\n(" + dataBean_search.getData().getLegalList().size() + ")");
                }
                initFragment(selectTab);

            }

            @Override
            public void onFailure(int requestCode, String str) {
                DialogUtil.dialog_ios.dismiss();
                canReq = true;
            }
        });
    }
}
